package com.cn.cs;
import java.sql.*;
import static java.lang.Class.forName;
//连接两个表
public class work1 {
    public static void main(String[] args)  {
        try {
            forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql1","root","root");
            //获取操作数据库的对象
            PreparedStatement pst =con.prepareStatement("select * from teacher a,student b\n" +
                    "        where a.class = b.class");
            //执行sql获取结果数据
            ResultSet rs =pst.executeQuery();
            //遍历结果集
            while(rs.next()){
                System.out.println("老师id:"+rs.getString("tid")+"\t学生:"+rs.getString("name")+"\t学生id:"+rs.getInt("id")+"\t课程名:"+rs.getInt("kcm"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

